package com.isoftstone.common.service.impl;

import com.isoftstone.common.dao.SysRoleMenuMapper;
import com.isoftstone.common.entity.SysRoleMenu;
import com.isoftstone.common.service.ISysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SysRoleMenuService implements ISysRoleMenuService {

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 更新menu菜单
     * @param roleId
     * @param menuIds
     * @throws Exception
     */
    @Transactional
    public void saveRoleMenus(String roleId, String menuIds) throws Exception  {


        if (StringUtils.isEmpty(menuIds)) {
            /**
             * 如果没有选择菜单,直接删除这个菜单的对应关系。
             */
            sysRoleMenuMapper.deleteRoleMenusByRoleId(roleId);
        }else {
            sysRoleMenuMapper.saveRoleMenus(roleId,menuIds);
            sysRoleMenuMapper.deleteRoleMenus(roleId,menuIds);
        }
    }
    /*
    * 商户端保存menu菜单
    * */
    public void shangHuSaveRoleMenus(String roleId, String menuIds) throws Exception  {

        if (StringUtils.isEmpty(menuIds)) {
            /**
             * 如果没有选择菜单,直接删除这个菜单的对应关系。
             */
            sysRoleMenuMapper.deleteRoleMenusByRoleId(roleId);
        }else {
            /*String str[]=menuIds.split(",");
            for(int i=0;i<str.length;i++){
                sysRoleMenuMapper.saveRoleMenus2(roleId,str[i]);
                sysRoleMenuMapper.deleteRoleMenus2(roleId,str[i]);
            }*/
            sysRoleMenuMapper.shangHuSaveRoleMenus(roleId,menuIds);
            sysRoleMenuMapper.shangHudeleteRoleMenus(roleId,menuIds);
        }
    }

    public List<String> getMenusByRoleId(String roleId) throws Exception {
        return sysRoleMenuMapper.getMenusByRoleId(roleId);
    }//getMenusByRoleIdOnlyLeaf

    @Override
    public List<String> getMenusByRoleIdOnlyLeaf(String roleId) throws Exception {
        return sysRoleMenuMapper.getMenusByRoleIdOnlyLeaf(roleId);
    }
    @Override
    public void deleteRoleMenu(String roleId) throws Exception {
        sysRoleMenuMapper.deleteRoleMenu(roleId);
    }

    @Override
    public void addRoleMenus(String roleid, String menuid) throws Exception {
        sysRoleMenuMapper.addRoleMenus(roleid,menuid);
    }


}
